package handler

import (
	"context"
	"encoding/json"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"shop_srvs/goods_srv/global"
	"shop_srvs/goods_srv/model"
	"shop_srvs/goods_srv/proto"
)

// GetAllCategoryList 商品分类(重要)
func (g *GoodsServer) GetAllCategoryList(context.Context, *emptypb.Empty) (*proto.CategoryListResponse, error) {
	categorys := []*model.Category{}

	global.DB.Where(&model.Category{Level: 1}).Preload("SubCategory.SubCategory").Find(&categorys)
	b, _ := json.Marshal(&categorys)
	return &proto.CategoryListResponse{
		JsonData: string(b),
	}, nil
}

// GetSubCategory 获取子分类(重要)
func (g *GoodsServer) GetSubCategory(ctx context.Context, req *proto.CategoryListRequest) (*proto.SubCategoryListResponse, error) {
	var category model.Category
	if res := global.DB.First(&category, req.Id); res.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}

	var subCategorys []*model.Category
	var categoryInfoResponses []*proto.CategoryInfoResponse
	global.DB.Where(&model.Category{ParentCategoryID: req.Id}).Find(&subCategorys) //父id为req.id，找到子分类
	for _, subCategory := range subCategorys {
		categoryInfoResponses = append(categoryInfoResponses, &proto.CategoryInfoResponse{
			Id:             subCategory.ID,
			Name:           subCategory.Name,
			ParentCategory: subCategory.ParentCategoryID, //这是int32 id
			Level:          subCategory.Level,
			IsTab:          subCategory.IsTab,
		})
	}
	return &proto.SubCategoryListResponse{
		Info: &proto.CategoryInfoResponse{ //自己
			Id:             category.ID,
			Name:           category.Name,
			ParentCategory: category.ParentCategoryID,
			Level:          category.Level,
			IsTab:          category.IsTab,
		},
		SubCategorys: categoryInfoResponses, //儿子
	}, nil
}
func (g *GoodsServer) CreateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.CategoryInfoResponse, error) {
	category := model.Category{
		Name:  req.Name,
		Level: req.Level,
		IsTab: req.IsTab,
	}
	if req.Level != 1 {
		//去查询父类是否存在
		parentCategory := model.Category{}
		parentCategory.ID = req.ParentCategory
		if res := global.DB.First(&parentCategory); res.RowsAffected != 0 {
			category.ParentCategoryID = req.ParentCategory
		}
	}
	global.DB.Model(&model.Category{}).Create(&category)
	return &proto.CategoryInfoResponse{
		Id: category.ID,
	}, nil
}
func (g *GoodsServer) DeleteCategory(ctx context.Context, req *proto.DeleteCategoryRequest) (*emptypb.Empty, error) {
	if res := global.DB.Delete(&model.Category{}, req.Id); res.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}
	return &emptypb.Empty{}, nil
}
func (g *GoodsServer) UpdateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*emptypb.Empty, error) {
	category := model.Category{}
	if res := global.DB.First(&category, req.Id); res.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}

	category.Name = req.Name
	category.ParentCategoryID = req.ParentCategory
	category.Level = req.Level
	if res := global.DB.Save(&category); res.Error != nil {
		return nil, status.Errorf(codes.Internal, res.Error.Error())
	}
	return &emptypb.Empty{}, nil
}
